package com.nlecloud.bc.controller.contract;

import com.nlecloud.bc.controller.contract.dto.InfringementDTO;
import com.nlecloud.bc.model.PageResult;
import com.nlecloud.bc.model.Result;
import com.nlecloud.bc.service.contract.IInfringementContractService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author warrior
 * @description 侵权合约API
 */
@RestController
public class InfringementContractController {

    @Autowired
    private IInfringementContractService infringementContractService;

    /**
     * 新增侵权信息
     *
     * @param infringementDTO 侵权信息传输封装对象
     * @param request
     * @return
     */
    @PostMapping("contract/infringements")
    public Result addInfringement(@RequestBody InfringementDTO infringementDTO, HttpServletRequest request) throws Exception {
        boolean success = infringementContractService.addInfringement(infringementDTO.getTitle(), infringementDTO.getUrl(), request);
        if (!success) {
            return Result.failed("新增侵权信息失败");
        }
        return Result.succeed("新增侵权信息成功");
    }

    /**
     * 获取侵权信息列表
     *
     * @param index 从第几条数据查询
     * @param size  查询条数
     * @return
     */
    @GetMapping("contract/infringements")
    public Result<PageResult<InfringementDTO>> listInfringement(@RequestParam("index") int index, @RequestParam("size") int size, HttpServletRequest req) throws ContractException {
        PageResult<InfringementDTO> infringementDTOS = infringementContractService.listInfringement(index, size, req);
        return Result.succeed(infringementDTOS);
    }

}
